var FG;
if (!FG)
    FG = {};

FG.geometryUriToFeatureType = function(geometryUri) {
    //TODO: more rules
    if (geometryUri.indexOf(':') > -1) {
        // ad:... etc.
        // TODO: 效率问题. 可能应该改为字典
        if (
            /^ad:/.test(geometryUri) ||
            /^guo:/.test(geometryUri) ||
            /^rgn:/.test(geometryUri) ||
            /^sea:/.test(geometryUri) 
        ) {
            return 'MultiPolygon';
        } else if (
            /^pad:/.test(geometryUri) ||
            /^p:/.test(geometryUri) 
        ) {
            return 'Point';
        } else if (
            /^rd:/.test(geometryUri) ||
            /^rl:/.test(geometryUri) ||
            /^av:/.test(geometryUri) ||
            /^sh:/.test(geometryUri)     
        ) {
            return 'MultiLine';
        }
    } else {
        // A..., P..., L... etc
        switch(geometryUri[0]) {
            case 'A':
                return 'MultiPolygon';
                break;
            case 'P':
                return 'Point';
                break;
            case 'L':
                return 'MultiLine';
                break;
        }
    }
};

FG.defaultPItemDBFeatureOptsParser = function(opts) {
    if (opts.dbtype !== 'pitem') {
        return;
    }
    if (!_.has(opts, 'type') || !_.contains(['Feature', 'UriFeature', 'Point', 'Rect'], opts.type)) {
        return;
    }
    
    var pitemOpts = { type: 'pitemOpts' };
    
    if (opts.type === 'Feature') {
        var g = opts.geometry;
        var pitemAttrs = _.omit(opts.properties, 'public', 'tag', 'index');
        switch (g.type) {
            case 'Point':
                _.extend(pitemOpts, {
                    item_type: 'Point', 
                    info: {lon: g.coordinates[0], lat: g.coordinates[1]}
                }, pitemAttrs);
                if (!pitemOpts.desc) {
                    pitemOpts.desc = "经纬度: " + pitemOpts.info.lat.toFixed(5) + '°N '
                        + pitemOpts.info.lon.toFixed(5) + '°E';
                }
                break;
            case 'Polygon':
            case 'MultiPolygon':
                _.extend(pitemOpts, {
                    item_type: 'MultiPolygon',
                    info: opts
                }, pitemAttrs);
                break;
            // case 'LineString':
            //     result = {
            //         featureType: ''
            //     };
            //     break;
        }
        
    } else if (opts.type === 'UriFeature') {
        var guri = opts.geometry_uri;
        var pitemAttrs = _.omit(opts.properties, 'public', 'tag', 'index');
        var featureType = FG.geometryUriToFeatureType(guri);
        var pitemType;
        switch (featureType) {
            case 'Point':
                pitemType = 'Point';
                break;
            case 'Polygon':
            case 'MultiPolygon':
                pitemType = 'MultiPolygon';
                break;
        }
        
        _.extend(pitemOpts, {item_type: pitemType}, pitemAttrs, {info: {geometry_uri: guri}});
    } else if (opts.type === 'Point') {
        var center = opts.center,
            pitemAttrs = _.omit(opts.properties, 'public', 'tag', 'index', 'center');
        _.extend(pitemOpts, {
            item_type: 'Point',
            info: center
        }, pitemAttrs);
        if (!pitemOpts.desc) {
            pitemOpts.desc = "经纬度: " + pitemOpts.info.lat.toFixed(5) + '°N '
                + pitemOpts.info.lon.toFixed(5) + '°E';
        }
    } else if (opts.type === 'Rect') {
        var b = opts.bounds;
            pitemAttrs = _.omit(opts.properties, 'public', 'tag', 'index', 'bounds');

        _.extend(pitemOpts, {
            item_type: 'Rect',
            info: opts.bounds
        }, pitemAttrs);
        if (!pitemOpts.desc) {
            pitemOpts.desc = '经纬度: ' + b.lat1.toFixed(5) + '~' + b.lat2.toFixed(5) + '°N ' +
                b.lon1.toFixed(5) + '~' + b.lon2.toFixed(5) + '°E';
        }
    }
    
    // if (_.has(pitemAttrs, 'adcode')) {
    //     pitemOpts.info.adcode = pitemAttrs.adcode;
    // };
        
    return pitemOpts;
};